﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model.EntityModel;
using DataAccess;
using CustomControls;
using System.Collections.ObjectModel;

namespace Project
{
    /// <summary>
    /// Interaction logic for RequestingMedicalContent.xaml
    /// </summary>
    public partial class RequestingMedicalContent : UserControl
    {
        SearchComplaintWindow sc;
        Complaint c = new Complaint();
        Model.EntityModel.RequestingMedical r = new Model.EntityModel.RequestingMedical();
        Actor actorVictim = new Actor();
        Person victim = new Person();
        public RequestingMedicalContent(long id)
        {
            InitializeComponent();
            UIWindowManager.SetWindowTitle(this, "Brigada De Protección a la Familia - Registrar Solicitud Evaluacion Medica");
            c.Id = id;
            lblCaseNumber.Content = c.Id;
            lblCaseDate.Content = DateTime.Now.Date;
            TxtBoxMedicalCenter.AddValitationRule(new BlankValidRule());
            TxtBoxMedicalName.AddValitationRule(new BlankValidRule());
            TxtBoxMedicalPName.AddValitationRule(new BlankValidRule());
            TxtBoxMedicalCenter.AddValitationRule(new ShortStringRule(3));
            TxtBoxMedicalName.AddValitationRule(new ShortStringRule(3));
            TxtBoxMedicalPName.AddValitationRule(new ShortStringRule(3));
            TxtBoxMedicalMName.DeactivateBlankValidation();
            TxtBoxVictimMName.DeactivateBlankValidation();
        }

        public RequestingMedicalContent()
        {
            InitializeComponent();
            UIWindowManager.SetWindowTitle(this, "Brigada De Protección a la Familia - Registrar Solicitud Evaluacion Medica");
            TxtBoxMedicalCenter.AddValitationRule(new BlankValidRule());
            TxtBoxMedicalName.AddValitationRule(new BlankValidRule());
            TxtBoxMedicalPName.AddValitationRule(new BlankValidRule());
            TxtBoxMedicalCenter.AddValitationRule(new ShortStringRule(3));
            TxtBoxMedicalName.AddValitationRule(new ShortStringRule(3));
            TxtBoxMedicalPName.AddValitationRule(new ShortStringRule(3));
            TxtBoxMedicalMName.DeactivateBlankValidation();
            TxtBoxVictimMName.DeactivateBlankValidation();
        }
       
        private bool CheckHasErrors()
        {
            bool res = false;
            foreach (var control in Content.Children)
            {                
                    foreach (var ctrl in (Content as Grid).Children)
                    {
                        TextBox tb = ctrl as TextBox;
                        if (tb != null)
                        {
                            tb.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                            if (Validation.GetHasError(tb))
                                res = true;
                        }                      
                    }                
            }
            return res;
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bool hasErrors = CheckHasErrors();
                if (!hasErrors)
                {
                    r.MedicalName = TxtBoxMedicalName.Text;
                    r.MedicalPaternalName = TxtBoxMedicalPName.Text;
                    r.MedicalMaternalName = TxtBoxMedicalMName.Text;
                    r.MedicalCenter = TxtBoxMedicalCenter.Text;
                    r.IdComplaint = c.Id;
                    r.RegistrationDate = DateTime.Now;
                    r.Status = true;
                    Model.EntityModel.RequestingMedical exist = RequestingMedicalDataAccess.GetRequestMedicalByComplaint(c.Id);
                    if (exist == null)
                    {
                        r = RequestingMedicalDataAccess.InsertRequest(r);
                        MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show("Solicitud Medica Registrada! Desea imprimirla?", "Confirmacion", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            var items = new ObservableCollection<UserControl>();
                            items.Add(new PrintRequestingMedical(c.Id));
                            var a = new PrintPreviewDialogV2(items);
                            a.ShowDialog();
                        }
                        else
                        {
                            Xceed.Wpf.Toolkit.MessageBox.Show("Puede imprimir la solicitud de atencion medica posteriormente, entrando a Ver Denuncias, click sobre la denuncia selecciona la opción Ver Solicitud de Evaluacion Medica",
                                                                "Información!", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                        UIWindowManager.CleanWindow(this);
                    }
                    else
                    {
                        Xceed.Wpf.Toolkit.MessageBox.Show("Ya se registro una solicitud de evaluacion medica para esta denuncia, en caso de abuso reiterado registre una nueva denuncia", "Aviso!", MessageBoxButton.OK, MessageBoxImage.Information);
                        UIWindowManager.CleanWindow(this);
                    }                  
                }
                else
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show("Errores en los campos, verifique la informacion introducida", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    UIWindowManager.CleanWindow(this);
                }
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("Lo sentimos, no se pudieron guardar los datos de la solicitud de atencion medica", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void getComplaintEventHandler(object sender, RoutedEventArgs e)
        {
            try
            {

               c.Id = (sc.Content.Children[0] as SearchComplaint).SelectedComplaintId;
                actorVictim=PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Victima",c.Id);
                victim=PersonAndActorDataAccess.GetPersonById(actorVictim.IdPerson);
                TxtBoxVictimName.Text = victim.Name;
                TxtBoxVictimPName.Text = victim.PaternalName;
                TxtBoxVictimMName.Text = victim.MaternalName;
                sc.Close();
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("Ocurrio un error al obtener datos del empleado", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }       

        private void TxtBoxVictimName_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                sc = new SearchComplaintWindow();
                (sc.Content.Children[0] as SearchComplaint).listViewComplaints.MouseDoubleClick += (getComplaintEventHandler);
                sc.ShowDialog();
                TxtBoxVictimName.Focusable = false;
            }
            catch
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("La denuncia ya fue escogida", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }

        }

        private void TxtBoxVictimPName_GotFocus(object sender, RoutedEventArgs e)
        {
            TxtBoxVictimName_GotFocus(sender, e);
            TxtBoxVictimPName.Focusable = false;
        }

        private void TxtBoxVictimMName_GotFocus(object sender, RoutedEventArgs e)
        {
            TxtBoxVictimName_GotFocus(sender, e);
            TxtBoxVictimMName.Focusable = false;
        }

    }
}
